# coding: utf-8
# 文件名称: uibot前奏.py
# 创建时间: 2021/7/1 11:13

# coding: utf-8
# 文件名称: json解析_获取商品链接.py
# 创建时间: 2021/7/1 0:03

import json
import csv
import numpy as np

fild = r'D:\python\pdd\pdd.txt'

# 存储所有小于0.7元的商品到本地
all_goods_id = open('all_goods_id.csv', 'a+', encoding='utf-8')

f = open(fild, 'r', encoding='utf-8')
text = f.read()
f.close()

# 判断最后一个字符是否是逗号，如果是则删除
if text.endswith(','):
    text = text.rstrip(',')

fb = '[' + text + ']'

persons = json.loads(fb)

lists = []
fw = open('D:\python\pdd\pdd.csv', 'a+', encoding='utf-8')

for i in persons:

    datas = i['data']['goods_list']
    for data in datas:

        if 'goods_id' in data:
            goods_id = data['goods_id']
            mall_id = data['mall_id']
            jiage = float(data['price'] / 100)
            if jiage <= 0.7:
                all_goods_id.write(str(goods_id))
                all_goods_id.write('\n')
                goods = {
                    'url': f'https://mobile.yangkeduo.com/chat_detail.html?mall_id={mall_id}&goods_id={goods_id}&cat_id_1=16901&page_from=0&is_full_reduction=false&is_full_return=false&refer_page_name=goods_detail&refer_page_id=10014_1625074923468_7n5946ogyw&refer_page_sn=10014',
                    'goods_id':goods_id,
                    'mall_id':mall_id,
                    'jiage':jiage
                }

                # 读取之前保存的所有商品
                OFF = True
                print('=====')
                with open('all_goods_id.csv', 'r', encoding='utf-8') as fb:
                    for f in fb.readlines():
                        if '1202109933' in f.strip():
                            OFF = False
                            break

                if OFF:
                    lists.append(goods)

                # fw.write('http://mobile.yangkeduo.com/goods.html?goods_id=' + str(goods_id))
                # fw.write('\n')
        else:
            goods_id = data['goods_list'][0]['goods_id']
            jiage = data['goods_list'][0]['price'] / 100
            if jiage <= 0.7:

                all_goods_id.write(str(goods_id))
                all_goods_id.write('\n')

                goods = {
                    'url': f'https://mobile.yangkeduo.com/chat_detail.html?mall_id={mall_id}&goods_id={goods_id}&cat_id_1=16901&page_from=0&is_full_reduction=false&is_full_return=false&refer_page_name=goods_detail&refer_page_id=10014_1625074923468_7n5946ogyw&refer_page_sn=10014',
                    'goods_id': goods_id,
                    'mall_id': mall_id,
                    'jiage':jiage
                }

                # 读取之前保存的所有商品
                OFF = True
                print('=====')
                with open('all_goods_id.csv', 'r', encoding='utf-8') as fb:
                    for f in fb.readlines():
                        if '1202109933' in f.strip():

                            OFF = False
                            break


                if OFF:
                    lists.append(goods)



print(f'低于1元的商品共找到：{len(lists)}')
fw.close()
all_goods_id.close()

print(lists)






headers = ['url', 'goods_id', 'mall_id', 'jiage']
with open('pdd.csv', 'w', encoding='utf-8', newline='') as f: # 如果出现多余空行，则使用newline参数将值设为空
    w = csv.DictWriter(f, headers) # 第2个参数，传递表头数据
    #w.writeheader() # 写入表头数据
    w.writerows(lists)




